package org.vestbjergbyggecenter.Controller;

import java.util.ArrayList;

import org.vestbjergbyggecenter.Model.*;

public class EmployeeCtr {
	private EmployeeRecords employees; 
	private static EmployeeCtr instance = null;  
    
	//constructor
    public EmployeeCtr()
    {
    	employees=EmployeeRecords.getInstance();
    }
    
    //employee control instance creation
    public static EmployeeCtr getInstance()
    {
            if(instance==null)
            {
            	instance=new EmployeeCtr();
            }
            return instance;
    }
    
    //create sales assistant and add him to the employees Array List
    public void createSalesAssistant(int employeeId, String employeeName, long employeeCPR, String employeeAddress, long employeePostalCode, String employeeCity, 
			String employeePhone, String employeeUsername, String employeePassword, int employeeSalary)
    {
    	SalesAssistant salesAssistant = new SalesAssistant(employeeId,  employeeName,  employeeCPR,  employeeAddress,  employeePostalCode,  employeeCity, 
        			 employeePhone,  employeeUsername,  employeePassword,  employeeSalary);
    	employees.addEmployee(salesAssistant);
    }
    
    //create manager and add him to the employees Array List
    public void createManager(int employeeId, String employeeName, long employeeCPR, String employeeAddress, long employeePostalCode, String employeeCity, 
			String employeePhone, String employeeUsername, String employeePassword, int employeeSalary, String department)
    {
    	Manager manager=new Manager(employeeId,  employeeName,  employeeCPR,  employeeAddress,  employeePostalCode,  employeeCity, 
        			 employeePhone,  employeeUsername,  employeePassword,  employeeSalary, department);
    	employees.addEmployee(manager);
    }   
    
    //method to get an employee based on his id
    public Employee getEmployee(int id)
    {
        Employee employee = employees.getEmployee(id);
        return employee;
    }
    
    //method to get the employee Array List
    public ArrayList<Employee> getEmployees()
    {
            return employees.getEmployees();
    }
    
    //method to print employee data based on its id
    public void printEmployee(int id)
    {
            Employee employee = getEmployee(id);
            if(employee != null)
                    employee.print();
    }
    
    //print the members of the employees Array List
    public void printEmployees()
    {
    	employees.viewEmployeeList();
    }
    
    //print the sales assistants
    
    
    //method to update a sales assistant based on his id
    public void updateSalesAssistant(int employeeId, String employeeName, long employeeCPR, String employeeAddress, long employeePostalCode, String employeeCity, 
			String employeePhone, String employeeUsername, String employeePassword, int employeeSalary)
    {
    	Employee employee=getEmployee(employeeId);
    	if(employee==null)
    	{
    		System.out.println("Please insert a valid employee ID.");
    	}
    	else
    	{
    		if(employee.isSalesAssistant()!=true)
    		{
    			System.out.println("The employee with the ID " + employee.getId() + " is not a sales assistant.");
    			System.out.println("Please insert a valid sales assistant ID.");
    		}
    		else
    		{
    			employee.setName(employeeName);
    	        employee.setCPR(employeeCPR);
    	        employee.setAddress(employeeAddress);
    	        employee.setPostalCode(employeePostalCode);
    	        employee.setCity(employeeCity);
    	        employee.setPhone(employeePhone);
    	        employees.updateEmployee(employeeId, employeeUsername, employeePassword, employeeSalary);
    		}
    	}        
    }
    
    //method to update a manager based on his id
    public void updateManager(int employeeId, String employeeName, long employeeCPR, String employeeAddress, long employeePostalCode, String employeeCity, 
			String employeePhone, String employeeUsername, String employeePassword, int employeeSalary, String department)
    {
    	Employee employee=getEmployee(employeeId);
    	if(employee==null)
    	{
    		System.out.println("Please insert a valid employee ID.");
    	}
    	else
    	{
    		if(employee.isManager()!=true)
    		{
    			System.out.println("The employee with the ID " + employee.getId() + " is not a manager.");
    			System.out.println("Please insert a valid sales assistant ID.");
    		}
    		else
    		{
    			employee.setName(employeeName);
    	        employee.setCPR(employeeCPR);
    	        employee.setAddress(employeeAddress);
    	        employee.setPostalCode(employeePostalCode);
    	        employee.setCity(employeeCity);
    	        employee.setPhone(employeePhone);
    	        employees.updateEmployee(employeeId, employeeUsername, employeePassword, employeeSalary);
    	        
    	        Manager newManager=new Manager(employee.getEmployeeID(), employee.getName(), employee.getCPR(), employee.getAddress(), employee.getPostalCode(), 
            			employee.getCity(), employee.getPhone(), employee.getUsername(), employee.getPassword(), employee.getSalary(), department);
    			employee=newManager;   	        
    		}
    	}
    }
    
    //method to remove an employee based on his id.
    public void deleteEmployee(int id)
    {
    	if(getEmployee(id)==null)
    	{
    		System.out.println("Please insert a valid employee ID.");
    	}
    	else
    	{
    		employees.removeEmployeeId(id);
    	}
    }  
	
	//method to check what an employee is based on his id
	public void checkEmployeeCategory(int id)
	{
		Employee employee=employees.getEmployee(id);
		if(employee==null)
    	{
    		System.out.println("Please insert a valid employee ID.");
       	}
		else
		{
			if(employee.isManager())
			{
				System.out.println("The employee with the ID " + employee.getId() +  " is a manager.");				
			}
			else if(employee.isSalesAssistant())
			{
				System.out.println("The employee with the ID " + employee.getId() + " is a sales assistant.");
			}
			else
			{
				System.out.println("The employee with the ID " + employee.getId() + " has not yet been attributed a position in the company.");
			}
		}
	}
	
	//method to see the norm of a salesman based on his id
	public void seeSalesAssistantNorm(int id)
	{
		Employee employee=employees.getEmployee(id);
		if(employee==null)
    	{
    		System.out.println("Please insert a valid employee ID.");
       	}
		else
		{
			if(employee.isSalesAssistant()!=true)
			{
				System.out.println("The employee with the ID " + employee.getId() +" is not a sales assistant.");
			}
			else
			{						
				SalesAssistant wantedSalesAssistant=new SalesAssistant(employee.getEmployeeID(), employee.getName(), employee.getCPR(), employee.getAddress(), employee.getPostalCode(), 
	        			employee.getCity(), employee.getPhone(), employee.getUsername(), employee.getPassword(), employee.getSalary());
	    		System.out.println("The sales assistant with the ID " + " has a norm of " + wantedSalesAssistant.calculateNorm(id));
			}
		}
	}

}
